package com.punai.config.sqlitedao;

import com.punai.common.exception.ServiceException;
import com.punai.common.utils.sqlite.SqliteUtils;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.domain.ConfigFormatCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class ConfigFormatCategoryRepository {
	private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	@Autowired
	private SqliteUtils sqliteUtils;

	public ConfigFormatCategory save(ConfigFormatCategory formatCategory) {
		String sql="insert "
				+ "into "
					+ "tab_config_formatcategory"
					+ "(create_date, edit_state, last_modify, sreach_key, version, code, name, parent, id,CLASS_IDS) "
					+ "values"
					+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		//initSqliteConnection();
		 Object[] args=new Object[]{
				 simpleDateFormat.format(new Date()),
				 PnConfigConstants.EDITSTATE.SAVESTATE.getType(),
				 simpleDateFormat.format(new Date()),
				 formatCategory.toString(),
				 PnConfigConstants.ENTITY_VERSION,
				 formatCategory.getCode(),
				 formatCategory.getName(),
				 formatCategory.getPid(),
				 formatCategory.getId(),
				 formatCategory.getClassIds()
		 };
		  int rownumber= sqliteUtils.execute(sql,args);
		  if(rownumber==0) {
			  throw new ServiceException("sqlite.message.updateException");
		  }
		return formatCategory;
	}

	public ConfigFormatCategory update(ConfigFormatCategory formatCategory) throws ParseException {
		String sql="update "
					+ "tab_config_formatcategory "
				+ "set "
					+ "edit_state=?,"
					+ "last_modify=?,"
					+ "sreach_key=?,"
					+ "version=?,"
					+ "code=?,"
					+ "name=?,"
					+ "parent=?,CLASS_IDS=? "
					+""
				+ "where id=?";
		ConfigFormatCategory configFormatCategory = findOneBySqlite(formatCategory.getId());
		if (configFormatCategory != null) {
			Object[] args=new Object[]{
					PnConfigConstants.EDITSTATE.UPDATESTATE.getType(),
					simpleDateFormat.format(new Date()),
					formatCategory.toString(),
					configFormatCategory.getVersion() + 1,
					formatCategory.getCode(),
					formatCategory.getName(),
					formatCategory.getPid(),
					formatCategory.getClassIds(),
					formatCategory.getId()
			};
			int rownumber= sqliteUtils.execute(sql,args);
			if(rownumber==0) {
				throw new ServiceException("sqlite.message.updateException");
			}
		} else {
			throw new ServiceException("基础库对应的数据不存在!");
		}
		return formatCategory;
	}

	public ConfigFormatCategory delete(ConfigFormatCategory formatCategory)  {
		String sql="delete from tab_config_formatcategory where id='"+formatCategory.getId()+"'";
		//initSqliteConnection();
		  int rownumber= sqliteUtils.execute(sql);
		  if(rownumber==0) {
			  throw new ServiceException("sqlite.message.updateException");
		  }
		return formatCategory;
	}

	public ConfigFormatCategory findOneBySqlite(String id) {
		String sql = "select * from tab_config_formatcategory where id = ?";
		return sqliteUtils.selectOneRecord(ConfigFormatCategory.class, sql,id);
	}


}
